package com.cqq.print.business.service.impl;


import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqq.print.business.dao.POcpProcessDao;
import com.cqq.print.business.dao.PProduceDao;
import com.cqq.print.business.dao.domain.POcpProcess;
import com.cqq.print.business.dao.domain.PProduce;
import com.cqq.print.business.service.bean.POcpProcessBean;
import com.cqq.print.constant.Constant;


/**
 * @author generated
 * @date 2015-07-01
 */


@Service(value="pOcpProcessService")
public class POcpProcessServiceImpl implements com.cqq.print.business.service.POcpProcessService{

             @Autowired
             private POcpProcessDao pOcpProcessDao;
             @Autowired
             private PProduceDao pProduceDao;
             
			
			public POcpProcessBean queryOCPProcessByProId(int proId) {
				POcpProcess ocp = pOcpProcessDao.queryOCPProcessByProId(proId);
				if(ocp!=null){
					POcpProcessBean ocpBean = new POcpProcessBean();
					BeanUtils.copyProperties(ocp, ocpBean);
					return ocpBean;
				}
				return null;
			}

			
			public void savePOCPProcess(POcpProcessBean ocpBean) {
				if(ocpBean!=null&&ocpBean.getProId()!=null){
					POcpProcess ocp = new POcpProcess();
					BeanUtils.copyProperties(ocpBean, ocp);
					if(ocp.getId()==null||ocp.getId()<=0){
						pOcpProcessDao.insertSelective(ocp);
					}else{
						pOcpProcessDao.updateByPrimaryKeySelective(ocp);
					}
					//更新生产任务的oct状态
					if(isAllChecked(ocp)){
						PProduce pp = new PProduce();
						pp.setId(ocp.getProId());
						pp.setOctCheck(Constant.PRODUCE_OCPCHECK_DONE);
						pp.setUpdateDate(new Date());
						pProduceDao.updateByPrimaryKeySelective(pp);
					}
				}
				
			}

			private boolean isAllChecked(POcpProcess ocp){
				if(ocp.getCoverpriceCopy()==null||ocp.getCoverpriceCopy()<1)
					return false;
				if(ocp.getPagenumStamp()==null||ocp.getPagenumStamp()<1)
					return false;
				if(ocp.getSampleCoverContent()==null||ocp.getSampleCoverContent()<1)
					return false;
				if(ocp.getSampleCoverSize()==null||ocp.getSampleCoverSize()<1)
					return false;
				if(ocp.getSpineCovername()==null||ocp.getSpineCovername()<1)
					return false;
				return true;
			}
}